package com.zhuiYun.project.config;

import com.zhuiYun.project.api.seckill.interceptor.AccessLimitInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: HKY
 * @date: 2023年04月13日 9:17
 * @ClassName: InterceptorConfig
 * @Description: TODO
 */
@Configuration
//@EnableWebMvc ：给添加了@Configuration类导入SpringMVC的配置
@EnableWebMvc
public class InterceptorConfig implements WebMvcConfigurer {
    /**
     * @Author zcy
     * @Description 允许跨域
     * @Date 10:29 2023/3/10
     * @param registry
     * @Return void
     **/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 允许跨域访问路径
        registry.addMapping("/**")
                // 允许跨域访问的源
                .allowedOrigins("*")
                // 允许请求方式
                .allowedMethods("POST","GET","PUT","DELETE","OPTIONS")
                // 预检间隔时间
                .maxAge(168000)
                // 允许头部设置
                .allowedHeaders("*");
    }

    /**
     *
     * @author: HKY
     * @date: 2023/4/12 11:06
     * @param registry
     * @Description: 注册拦截器（限流拦截器）
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AccessLimitInterceptor()).addPathPatterns("/**");
    }

}
